package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysPostage;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysPostageService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("sys/postage")
@Api(tags = "系统资费管理")
@RestController
public class SysPostageController {

    @Autowired
    private SysPostageService sysPostageService;

    @ApiOperation(value = "查询系统资费列表")
    @GetMapping
    public JsonResult<List<SysPostage>> findList(Byte type) {
        LambdaQueryWrapper<SysPostage> queryWrapper = new LambdaQueryWrapper<SysPostage>()
                .eq(type != null, SysPostage::getType, type);
        queryWrapper.last("ORDER BY id");
        List<SysPostage> sysPostageList = sysPostageService.list(queryWrapper);
        return JsonResult.ok(sysPostageList);
    }


    @ApiOperation(value = "查询系统资费列表")
    @GetMapping("{id}")
    public JsonResult<SysPostage> getSysPostage(@PathVariable Long id) {
        SysPostage sysPostage = sysPostageService.getById(id);
        return JsonResult.ok(sysPostage);
    }


    @ApiOperation(value = "修改系统资费")
    @PutMapping
    public JsonResult addSysPostage(@RequestBody SysPostage sysPostage) {
        sysPostage.setCreateBy(SecurityUtils.getUserId());
        sysPostage.setLastUpdateTime(new Date());
        boolean update = sysPostageService.updateById(sysPostage);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
